package com.jindy.module.outgoingapproval.controller;

import com.jindy.common.annotation.Log;
import com.jindy.common.core.controller.BaseController;
import com.jindy.common.core.domain.AjaxResult;
import com.jindy.common.core.page.TableDataInfo;
import com.jindy.common.enums.BusinessType;
import com.jindy.common.utils.poi.ExcelUtil;
import com.jindy.module.outgoingapproval.domain.SyOutgoingApproval;
import com.jindy.module.outgoingapproval.service.ISyOutgoingApprovalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 外出审批Controller
 * 
 * @author jindy
 * @date 2023-05-12
 */
@RestController
@RequestMapping("/activiti/outgoingApproval")
public class SyOutgoingApprovalController extends BaseController
{
    private static final Logger LOGGER = LoggerFactory.getLogger(SyOutgoingApprovalController.class);
    @Autowired
    private ISyOutgoingApprovalService syOutgoingApprovalService;

    /**
     * 查询外出审批列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:outgoingApproval:list')")
    @GetMapping("/list")
    public TableDataInfo list(SyOutgoingApproval syOutgoingApproval)
    {
        startPage();
        List<SyOutgoingApproval> list = syOutgoingApprovalService.selectSyOutgoingApprovalList(syOutgoingApproval);
        return getDataTable(list);
    }

    /**
     * 导出外出审批列表
     */
    @PreAuthorize("@ss.hasPermi('activiti:outgoingApproval:export')")
    @Log(title = "外出审批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SyOutgoingApproval syOutgoingApproval)
    {
        List<SyOutgoingApproval> list = syOutgoingApprovalService.selectSyOutgoingApprovalList(syOutgoingApproval);
        ExcelUtil<SyOutgoingApproval> util = new ExcelUtil<SyOutgoingApproval>(SyOutgoingApproval.class);
        util.exportExcel(response, list, "外出审批数据");
    }

    /**
     * 获取外出审批详细信息
     */
    @PreAuthorize("@ss.hasPermi('activiti:outgoingApproval:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(syOutgoingApprovalService.selectSyOutgoingApprovalById(id));
    }

    /**
     * 新增外出审批
     */
    @PreAuthorize("@ss.hasPermi('activiti:outgoingApproval:add')")
    @Log(title = "外出审批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SyOutgoingApproval syOutgoingApproval)
    {
        return toAjax(syOutgoingApprovalService.insertSyOutgoingApproval(syOutgoingApproval));
    }

    /**
     * 修改外出审批
     */
    @PreAuthorize("@ss.hasPermi('activiti:outgoingApproval:edit')")
    @Log(title = "外出审批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyOutgoingApproval syOutgoingApproval)
    {
        return toAjax(syOutgoingApprovalService.updateSyOutgoingApproval(syOutgoingApproval));
    }

    /**
     * 删除外出审批
     */
    @PreAuthorize("@ss.hasPermi('activiti:outgoingApproval:remove')")
    @Log(title = "外出审批", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(syOutgoingApprovalService.deleteSyOutgoingApprovalByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "外出审批", businessType = BusinessType.UPDATE)
    @PostMapping( "/submitApply/{id}")
    @ResponseBody
    public AjaxResult submitApply(@PathVariable Long id) {
        try {
            SyOutgoingApproval outgoingApproval = syOutgoingApprovalService.selectSyOutgoingApprovalById(id);
            syOutgoingApprovalService.submitApply(outgoingApproval);
        } catch (Exception e) {
            LOGGER.error("外出审批提交申请出错",e);
            return error("外出审批提交申请出错：" + e.getMessage());
        }
        return success();
    }
}
